#include "Player.h"

using namespace cocos2d;

/*
CCSprite* Player::createPlayer()
{
    Player *pSprite = new Player();
    if (pSprite && pSprite->init())
    {
        pSprite->autorelease();
        return pSprite;
    }
    CC_SAFE_DELETE(pSprite);
    return NULL;
}
 */


Player::Player()
{
    init();
}

bool Player::init()
{
    _totalCredit = 0;
    _fCreditCount = 0;
    
	CCSize size = CCDirector::sharedDirector()->getWinSize();
    _pBody = CCSprite::create("C_default.png");
	//_pBody->initWithFile("C_default.png");
    _pBody->setPosition( ccp(0.0f, 0.0f) );
    this->addChild(_pBody);
	
//	this->schedule(schedule_selector(Player::update), 0.5);

    const int count = 4;
    CCArray* frames = CCArray::createWithCapacity(count);
    CCSprite* sprite;
    CCSpriteFrame* frame;
    
    sprite = CCSprite::spriteWithFile("C_left.png");
    frame = CCSpriteFrame::frameWithTexture(sprite->getTexture(), sprite->getTextureRect());
    frames->addObject(frame);
    
    sprite = CCSprite::spriteWithFile("C_default.png");
    frame = CCSpriteFrame::frameWithTexture(sprite->getTexture(), sprite->getTextureRect());
    frames->addObject(frame);
    
    sprite = CCSprite::spriteWithFile("C_right.png");
    frame = CCSpriteFrame::frameWithTexture(sprite->getTexture(), sprite->getTextureRect());
    frames->addObject(frame);
    
    sprite = CCSprite::spriteWithFile("C_default.png");
    frame = CCSpriteFrame::frameWithTexture(sprite->getTexture(), sprite->getTextureRect());
    frames->addObject(frame);
    
    CCAnimation* animation = CCAnimation::createWithSpriteFrames(frames, 0.5f);
    
    _pBody->runAction(CCRepeatForever::create(CCAnimate::create(animation)));
    
    _collisionBox = CCSprite::spriteWithFile("C_bound.png");
    _collisionBox->setPosition(ccp(0.0f, 35.0f));
    _collisionBox->setVisible(false);
    this->addChild(_collisionBox);
    
	return true;
}

void Player::setPosition(CCPoint position)
{
    _pBody->setPosition(position);
    
    _collisionBox->setPosition(ccpAdd(position,ccp(0.0f, 35.0f)));
}

CCPoint Player::position()
{
    return _pBody->getPosition();
}


void Player::update(float dt)
{
	_pBody->setPosition(ccpAdd(_pBody->getPosition(), ccpMult(_velocity, dt)));
    _collisionBox->setPosition(ccpAdd(_collisionBox->getPosition(), ccpMult(_velocity, dt)));

	if(_pBody->getPositionX() < -10.0f)
	{
		_pBody->setPosition(ccp(10.0f, _pBody->getPositionY()));
        _collisionBox->setPosition(ccp(10.0f, _collisionBox->getPositionY()));
	}
	else if(_pBody->getPositionX() > 310.0f)
	{
		_pBody->setPosition(ccp(310.0f, _pBody->getPositionY()));
        _collisionBox->setPosition(ccp(310.0f, _collisionBox->getPositionY()));
	}
}

void Player::move(enum player_move_type type)
{
	if(type == PLAYER_MOVE_TYPE_LEFT)
	{
		_velocity = ccp(-40.0f, 0.0);
	}
	else if(type == PLAYER_MOVE_TYPE_RIGHT)
	{
		_velocity = ccp(+40.0f, 0.0);
	}
}

CCRect Player::boundingBox()
{
    return _collisionBox->boundingBox();
}

void Player::setBlock(int blockType)
{
    if(blockType == 0)
    {
        _totalCredit += 4;
    }
    else if(blockType == 1)
    {
        _totalCredit += 3;
    }
    else if(blockType == 2)
    {
        _totalCredit += 2;
    }
    else if(blockType == 3)
    {
        _totalCredit += 1;
    }
    else if(blockType == 4)
    {
        //_totalCredit += 0;
        ++_fCreditCount;
    }
}

void Player::breakdown()
{
    _pBody->stopAllActions();
    _pBody->setTexture(CCTextureCache::sharedTextureCache()->addImage("C_final.png"));
}
